import java.io.FileOutputStream;
import java.io.StringReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;

public class HtmlToPDF {

  public static void main(String[] args ) {
    try {
      Document document = new Document(PageSize.LETTER);
      PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(args[0]+".pdf"));
      document.open();
      document.addAuthor("Real Gagnon");
      document.addCreator("Real's HowTo");
      document.addSubject("Thanks for your support");
      document.addCreationDate();
      document.addTitle("Please read this");

      HTMLWorker htmlWorker = new HTMLWorker(document);
 //     String str = "<table border=0 RULES=NONE FRAME=BOX><tr><td><img src='http://localhost/morsa_A2Hosting/img/logomorsa4.jpg'></td><td>  <table><tr><td>TIPO DE DOCUMENTO </td><td>Factura</td></tr>        <tr><td>SERIE - FOLIO </td><td>MZTMA 12965</td></tr>        <tr><td>FECHA Y HORA </td><td>2011-05-27T14:32:10</td></tr>        <tr><td>LUGAR EXPEDICION</td><td>MAZATLAN SINALOA</td></tr>        <tr><td>A&Ntilde;O Y NO DE EXPEDICION</td><td>2010 215817</td></tr>       <tr><td>NO. DE CERTIFICADO </td><td>00001000000101987388</td></tr>  </table></td></tr><tr><td>RAFAEL BUELNA 100-102 INF. PLAYAS MAZATLAN SINALOA</td><td>FECHA VENCIMIENTO </td></tr><tr><td>  <table border=1 RULES=NONE FRAME=BOX width=100%>     <tr><td><strong>CLIENTE:</strong>   FAUSTO REFACCIONES AUTOMOTRICES, S.A. DE C.V.</td></tr>     <tr><td><strong>DOMICILIO:</strong> EJERCITO MEXICANO 1016 FRACC.  SAN ANGEL</td></tr>     <tr><td><strong>POBLACION:</strong> MAZATLAN SINALOA</td></tr>     <tr><td><strong>RFC:</strong>       FRA860130R71&nbsp;&nbsp;&nbsp;&nbsp; <strong>CODIGO POSTAL:</strong> 82014</td></tr>  </table></td><td><table width=100% CELLPADDING=13 CELLSPACING=5 border=1 RULES=COLS FRAME=BOX ><tr><td>EMBARQUE</td><td>CONDICIONES</td></tr><tr><td>.</td><td>CONTADO</td></tr></table></td><tr><td colspan=3>   <table width=100% border=1 rules='cols' frame=box CELLPADDING=3 CELLSPACING=3>      <tr><td width=10%>&nbsp;</td><td width=20% >&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr>      <tr><td width=10%><strong>CANTIDAD</strong></td><td width=20% ><strong>NO. DE PIEZA</strong></td><td width=40%><strong>DESCRIPCION</strong></td><td width=15%><strong>PRECIO UNITARIO</strong></td><td width=15%><strong>IMPORTE</strong></td></tr>      <tr><td width=10%>&nbsp;</td><td width=20% >&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr> </td></tr></table><tr><td colspan=3>    <table width=100% border=1 rules='cols' frame=box CELLPADDING=3><tr><td width=10%>&nbsp;</td><td width=20%>&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr><tr><td width=10%>1</td><td width=20%>10570X-030</td><td width=40%>ANILLOS PARA MOTOR</td><td width=15% align=right>356.29</td><td width=15% align=right>356.29</td></tr><tr><td width=10%>4</td><td width=20%>822-030</td><td width=40%>PISTON PARA MOTOR</td><td width=15% align=right>142.85</td><td width=15% align=right>571.40</td></tr><tr><td width=10%>&nbsp;</td><td width=20%>&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr><tr><td width=10%>&nbsp;</td><td width=20%>&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr><tr><td width=10%>&nbsp;</td><td width=20%>&nbsp;</td><td width=40%>&nbsp;</td><td width=15%>&nbsp;</td><td width=15%>&nbsp;</td></tr></table></td></tr><tr><td colspan=3>   <table width=100% border=1 rules='cols' frame=box>      <tr><td width=20%><strong>SUMA</strong></td><td width=20% ><strong>DESCUENTO</strong></td><td width=20%><strong>SUB-TOTAL</strong></td><td width=20%><strong>I.V.A.</strong></td><td width=20%><strong>TOTAL</strong></td></tr>      <tr><td width=20%>&nbsp;</td><td width=20% >&nbsp;</td><td width=20%>&nbsp;</td><td width=20%>&nbsp;</td><td width=20%>&nbsp;</td></tr>      <tr><td width=20% align=center>992.63</td><td width=20% align=center>64.94</td><td width=20% align=center>927.69</td><td width=20% align=center>138.04</td><td width=20% align=center>1000.79</td></tr>      <tr><td width=20%>&nbsp;</td><td width=20% >&nbsp;</td><td width=20%>&nbsp;</td><td width=20%>&nbsp;</td><td width=20%>&nbsp;</td></tr>   </table></td></tr><tr><td colspan=3>   <table width=100% border=1 rules=none frame=box>      <tr><td> <strong>CANTIDAD CON LETRA:</strong></td></tr>      <tr><td>Un Mil pesos Con Setenta y Nueve Centavos M.N.</td></tr>   </table></td></tr><tr><td colspan=3>   <table width=100% border=0 rules=none frame=box>      <tr><td> &nbsp;</td></tr>      <tr><td> <strong>CADENA ORIGINAL</strong></td></tr>      <tr><td><font size=2>||2.0|MZTMA|12965|2011-05-27T14:32:10|215817|2010|ingreso|Pago en una sola exhibicion|CONTADO|927.69|64.94|1000.79|GMM991105IS8|GRUPO MORSA DE MEXICO S.A. DE C.V|RAFAEL BUELNA|100-102|INF. PLAYAS|MAZATLAN|MAZATLAN|SINALOA|MEXICO|82120|DONATO GUERRA|350|SUR|JORGE ALMADA|CULIACAN DE ROSALES|CULIACAN|SINALOA|MEXICO|80200|FRA860130R71|FAUSTO REFACCIONES AUTOMOTRICES, S.A. DE C.V.|EJERCITO MEXICANO|1016|FRACC.  SAN ANGEL|MAZATLAN|MAZATLAN|SINALOA|MEXICO|82014|1|10570X-030|ANILLOS PARA MOTOR|356.29|356.29|4|822-030|PISTON PARA MOTOR|142.85|571.40|IVA|16.00|138.04||</font></td></tr>      <tr><td> &nbsp;</td></tr>   </table></td></tr><tr><td colspan=3>   <table width=100% border=0 rules=none frame=box>      <tr><td> &nbsp;</td></tr>      <tr><td width=10%> <strong>SELLO DIGITAL</strong></td><td><font size=2>o54nkxhvmMFnW8/rP7Djaz9sr4NrqCM6S2oVJAVG/NpdJemol2U+lRRspg0IDp6og+dyWTCK4lrt58K6wq4aDRXGndijDx4NYkI8S8G1vFjIKXQXEYwUUiQIu8S3v88v7o0zOV8mGvs37ZRnnKnogB3FrIcj2g8ix/dGilCj+r0=</font></td></tr>      <tr><td> &nbsp;</td></tr>   </table></td></tr><tr><td colspan=3>   <table border=0 width=100%>      <tr><td> &nbsp;</td></tr>      <tr><td align=center>Este documento es una representaci&oacute;n impresa de un CFD </td></tr>      <tr><td> &nbsp;</td></tr>   </table></td></tr></table>";
     String str = args[1];      
      htmlWorker.parse(new StringReader(str));
      document.close();
      System.out.println("Done");
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

